<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * 模型基类
 */
class BaseModel extends Model
{
    public $timestamps = false;

    public function uploadImg(Request $request, $img)
    {
        $imgPath = null;
        if ($request->hasFile($img) && $request->file($img)->isValid()) {
            $file = $request->file($img);
            //验证图片格式
            $ext = strtolower($file->getClientOriginalExtension());
            if ($ext !== "jpg" && $ext !== "jpeg" && $ext !== "png") {
                return false;
            }
            //重命名图片并移动到目录
            $filename = md5_file($file->getPathname()) . "." . $ext;   //md5 + 扩展名
            $file->move('uploads/upload_imgs/', $filename);
            $imgPath = 'uploads/upload_imgs/' . $filename;
        }
        return $imgPath;
    }
}